package cz.ctu.android.DriveCommunicator.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import android.graphics.YuvImage;
import android.os.Environment;
import android.view.View.MeasureSpec;

import cz.ctu.android.DriveCommunicator.utils.DriverConstants.Users;

public class HandlerMessage {

	public ArrayList<Zprava> getConversetion(int id) {
		ArrayList<Zprava> list = new ArrayList<Zprava>();
		File root = Environment.getExternalStorageDirectory();
		File f = new File(root,id + ".txt");
		if (!f.exists()) {
			return null;
		}
		
		try {
			BufferedReader in = new BufferedReader(new FileReader(f));
			String line = null;
			while ((line = in.readLine()) != null) {
				String[] arr = line.split(";");
				int idZ = Integer.parseInt(arr[0]);
				int idSender = Integer.parseInt(arr[1]);
				String jmenoZ = arr[2];
				String prijmeniZ = arr[3];
				int fotkaZ = Integer.parseInt(arr[4]);
				String typZ = arr[5];
				Typ t = null;
				if(typZ.equals("ODESILATEL")) {
					t = Typ.ODESILATEL;
				} else {
					t = Typ.PRIJEMCE;
				}
				
				boolean prectenaZ = Boolean.parseBoolean(arr[6]);
				String datZ = arr[7];
				String zprava = arr[8];
				if(idSender == -1){
					
				}
				Zprava z = new Zprava(idZ,Users.getUserByID(idSender), t,
						prectenaZ, datZ, zprava);
				list.add(z);

			}
			in.close();
			//revert order
			ArrayList<Zprava> tempMessage = new ArrayList<Zprava>(list);
			list = new ArrayList<Zprava>();
			for(int i=tempMessage.size()-1;i>=0;i--){
				Zprava zprava = tempMessage.get(i);
				list.add(zprava);
			}
				
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}

	public void addMessage(Zprava zprava) throws IOException {
		int id = zprava.getId();
		File root = Environment.getExternalStorageDirectory();
		File f = new File(root,id + ".txt");
		if (!f.exists()) {
			f.createNewFile();
		}

		FileWriter fw = new FileWriter(f, true);
		String s = zprava.getId() + ";"+zprava.getSenderID() + ";" + zprava.getJmeno() + ";"
				+ zprava.getPrijmeni() + ";" + zprava.getCisloFotky() + ";"
				+ zprava.getTyp() + ";" + zprava.isPrectena() + ";"
				+ zprava.getDate() + ";" + zprava.getZprava() + "\r\n";
		fw.append(s);
		fw.close();
	}

	public void deleteMessage(Zprava zprava) {
		List<Zprava> list = getConversetion(zprava.getId());

		if (list == null)
			return;
		if (list.size() == 1) {
			deleteConversation(zprava.getId());
		}
		int temp = -1;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).equals(zprava)) {
				temp = i;
				break;
			}
		}

		if (temp != -1) {
			list.remove(temp);
		}

		File f = new File(zprava.getId() + ".txt");
		f.delete();
		try {
			f.createNewFile();
			for (Zprava zprava2 : list) {
				addMessage(zprava2);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return;
	}

	public void deleteConversation(int id) {
		File root = Environment.getExternalStorageDirectory();
		File f = new File(root,id + ".txt");
		if (f.exists()) {
			f.delete();
		}
	
	}
	
	public void markAsRead(int id){
		ArrayList<Zprava> messages = getConversetion(id);
		if(messages !=null && !messages.isEmpty()){
			deleteConversation(id);
			Iterator messageIt = messages.iterator();
			while(messageIt.hasNext()){
				Zprava zprava = (Zprava) messageIt.next();
				zprava.setPrectena(true);
				try{
					addMessage(zprava);
				}
				catch(IOException i){
					return;
				}
			}
		}
	}

}
